package com.dchh.android.suverynew.act;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dchh.android.suverynew.R;
import com.dchh.android.suverynew.base.SBaseActivity;
import com.dchh.android.suverynew.common.SPreferencesManager;
import com.dchh.android.suverynew.views.MapPopupWindow;
import com.dhcc.android.base.base.MSApplication;
import com.dhcc.android.base.db.DBManagerUtil;
import com.dhcc.android.base.utils.CommenUtil;
import com.dhcc.android.base.utils.Constants;
import com.dhcc.android.base.utils.FileUtil;

import java.util.List;
import java.util.Map;

/**
 * 预产填报
 *
 * @author zhoudan
 * @date 2016年3月23日
 */
public class YCReportActivity extends SBaseActivity {

    private RelativeLayout main;
    private RelativeLayout map_icon;

    private LinearLayout content;
    private LinearLayout save;

    private List<Map<String, String>> datas;

    private SharedPreferences sp;
    private String root;
    private String taskName;
    private String dbPath;
    private String table = "预产表";

    private String YFBHU;

    private DBManagerUtil dbManagerUtil;

    private boolean isEdit = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yield_yuchan_tianbao);
    }

    private String getTitleStr() {
        String s = Constants.selects.get(0).get("CUNMC") + "样方" + Constants.selects.get(1).get("YFBH");

        return s;
    }


    @Override
    protected void setTitle(TextView toolbar_title) {
        toolbar_title.setText(getTitleStr());
    }

    @Override
    public void initView() {
        root = SPreferencesManager.getValue(MSApplication.TASK_SAVE_PATH, FileUtil.getSavePath());
        taskName = getIntent().getStringExtra("taskName");
        YFBHU = getIntent().getStringExtra("YFBHU");

        dbPath = root + "/" + taskName + "/TASK/YIELD/DATA.db";
        dbManagerUtil = DBManagerUtil.getInstance(dbPath, this);

        initView();
        content = (LinearLayout) main.findViewById(R.id.content);
        save = (LinearLayout) main.findViewById(R.id.save_layout);

        datas = dbManagerUtil.getYCListDatas(table, dbPath, YFBHU);
        if (datas != null) {
            for (int i = 0; i < datas.size(); i++) {
                Map<String, String> map = datas.get(i);

                View v = LayoutInflater.from(this).inflate(R.layout.yield_yuchan_list_item, null);
                TextView zw = (TextView) v.findViewById(R.id.zwmc);
                TextView mj = (TextView) v.findViewById(R.id.zwmj);
                EditText cl = (EditText) v.findViewById(R.id.yjdc);

                zw.setText(map.get("ZWMC"));
                mj.setText("（" + map.get("ZWMJ") + "亩）");
                cl.setText(map.get("ZWCL"));

                cl.addTextChangedListener(new MyTextChange(i, cl));

                content.addView(v);
            }
        }


        save.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (isEdit) {
                    isEdit = false;
                    dbManagerUtil.ycSave(datas, table, dbPath, YFBHU);
                }

                CommenUtil.toast("保存成功", YCReportActivity.this);
            }
        });
    }


    private class MyTextChange implements TextWatcher {

        private int index;
        private EditText edit;

        public MyTextChange(int index, EditText edit) {
            this.index = index;
            this.edit = edit;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            String val = s.toString().trim();
            Map<String, String> map = datas.get(index);
            map.put("ZWCL", val);

            datas.remove(index);
            datas.add(index, map);

            isEdit = true;
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.yc_report_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.map) {
            MapPopupWindow mapwindow = new MapPopupWindow(YCReportActivity.this);
            mapwindow.show(item.getActionView(), Constants.ZRDKBITMAP);
        }
        return true;
    }
}
